package cn.lifeaid.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import cn.lifeaid.service.SysUserService;
import cn.lifeaid.utils.Constants;

public class SysUserFilter extends PathMatchingFilter {

	@Autowired
	private SysUserService userService;

	/**
	 * filterChainDefinitions中配置了 /** = user,sysUser
	 * 即所有的url都要进入该过滤器，onPreHandle方法是在执行跳转url前调用的方法
	 */
	@Override
	protected boolean onPreHandle(ServletRequest request,
			ServletResponse response, Object mappedValue) throws Exception {

		String username = (String) SecurityUtils.getSubject().getPrincipal();
		request.setAttribute(Constants.CURRENT_USER,
				userService.findByUsername(username));
		return true;
	}
}
